import ajax from '@util/ajax.ts'
import { TypeClassInfo } from '@src/interfaces/TypeClassInfo.ts'
import { Result } from '@src/interfaces/TypeResult.ts'
import { TypePageDTO } from '@src/interfaces/TypePageDTO.ts'
import { TypeUser } from '@src/interfaces/TypeUser.ts'

/**
 * 创建班级
 */
export async function createClassApi(classInfo: TypeClassInfo) {
  return ajax.post('/class-info', classInfo)
}

/**
 * 获取班级列表
 */
export function getClassListApi(params: {
  page: number
  pageSize: number
  name?: string
  ids?: string
}) {
  params.ids = params?.ids?.replace(/，/g, ',')
  return ajax
    .get('/class-info/list', {
      params,
    })
    .then((res) => {
      return res.data as Result<TypePageDTO<TypeClassInfo>>
    })
}

/**
 * 获取当前班级列表
 */
export function getCurrentClassApi() {
  return ajax.get('/class-info/current').then(({ data }) => {
    return data as Result<TypeClassInfo>
  })
}

/**
 * 修改班级信息
 */
export async function updateClassInfoApi(classInfo: TypeClassInfo) {
  return ajax.put('/class-info', classInfo).then(({ data }) => {
    return data as Result<any>
  })
}

export async function geyClassById(classId: number) {
  return ajax.get(`/class-info/${classId}`)
}

/**
 * 解散班级
 */
export async function deleteClassByIdApi(classId: number) {
  return ajax.delete(`/class-info/${classId}`).then(({ data }) => {
    return data as Result<any>
  })
}

/**
 * 获取当前班级的班级成员
 */
export async function getClassMemberApi(params: {
  page: number
  size: number
  name?: string
  account?: string
  phone?: string
}) {
  return ajax
    .get('/class-info/member', {
      params,
    })
    .then(({ data }) => {
      return data as Result<TypePageDTO<TypeUser>>
    })
}

export async function identify() {
  return ajax.get('/class-info/identify')
}

/**
 * 解封班级
 * @param classId 班级ID
 */
export function banClassApi(classId: number) {
  return ajax.put(`/class-info/ban/${classId}`).then(({ data }) => {
    return data as Result<any>
  })
}

/**
 * 禁封班级
 * @param classId 班级ID
 */
export function unbanClassApi(classId: number) {
  return ajax.put(`/class-info/unban/${classId}`).then(({ data }) => {
    return data as Result<any>
  })
}

/**
 * 退出班级
 */
export function exitClassApi() {
  return ajax.put('/class-info/exit').then(({ data }) => {
    return data as Result<any>
  })
}

/**
 * 加入班级
 */
export function joinClassApi(classId: number) {
  return ajax.put(`/class-info/join/${classId}`).then(({ data }) => {
    return data as Result<any>
  })
}
